package com.wmx.hb.fastmybatis;

import com.gitee.fastmybatis.core.PageInfo;
import com.wmx.hb.enums.ResultCode;
import com.wmx.hb.fastmybatis.pojo.BasPersonInfo;
import com.wmx.hb.fastmybatis.service.PersonInfoService;
import com.wmx.hb.pojo.ResultData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 使用 fastmybatis 操作
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2022/12/7 19:45
 */
@RestController
public class PersonIfoController {

    @Resource
    private PersonInfoService personInfoService;

    /**
     * 分页查询
     * http://localhost:8080/emp2/listPage
     * http://localhost:8080/emp2/listPage?pageIndex=1&pageSize=20
     *
     * @param pageIndex ：当前页码，必须大于等于1
     * @param pageSize  ：每页展示的条数，必须大于等于1
     * @return
     */
    @GetMapping("/emp2/listPage")
    public ResultData<PageInfo<BasPersonInfo>> listPage(@RequestParam(defaultValue = "1") int pageIndex,
                                                        @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<BasPersonInfo> pageInfo = personInfoService.listPage(pageIndex, pageSize);
        return new ResultData<>(ResultCode.SUCCESS, pageInfo, pageInfo.getTotal(), pageInfo.getPageIndex(), pageInfo.getPageSize());
    }

    /**
     * 查询指定部门下的员工
     * http://localhost:8080/emp2/listMapByDeptNo?deptNo=10
     *
     * @param deptNo
     * @return
     */
    @GetMapping("/emp2/listMapByDeptNo")
    public ResultData<List<Map<String, Object>>> listByDeptNo(@RequestParam int deptNo) {
        List<Map<String, Object>> listMapByDeptNo = personInfoService.listMapByDeptNo(deptNo);
        return new ResultData<>(ResultCode.SUCCESS, listMapByDeptNo);
    }

    /**
     * com.gitee.fastmybatis.core.query.Query 查询方法测试
     *
     * @return
     */
    @GetMapping("/emp2/queryTest")
    public ResultData queryTest() throws ParseException {
        personInfoService.queryTest();
        return new ResultData<>();
    }

    /**
     * 根据员工名称模糊查询
     * http://localhost:8080/emp2/loadByName
     *
     * @param ename
     * @return
     */
    @GetMapping("/emp2/loadByName")
    public ResultData<List> loadByName(String ename) {
        // select * from bas_person_info WHERE 1=2
        // WHERE ename like concat('%',concat(?,'%'))
        List<BasPersonInfo> deptList = personInfoService.loadByName(ename);
        return new ResultData<>(ResultCode.SUCCESS, deptList);
    }

    /**
     * 新增保存，忽略null字段
     * http://localhost:8080/emp2/save
     * <p>
     * {"ename":"蚩尤后裔","job":"clerk","mgr":7902,"hiredate":"1992-05-05 12:00:12","sal":8200,"comm":null,"deptno":2}
     * </p>
     *
     * @param basPersonInfo
     * @return
     */
    @PostMapping("/emp2/save")
    public ResultData<Integer> save(@RequestBody BasPersonInfo basPersonInfo) {
        personInfoService.save(basPersonInfo);
        // 返回添加后的主键值
        return new ResultData<>(basPersonInfo.getEmpno());
    }

    /**
     * 更新，更新所有字段
     * http://localhost:8080/emp2/update
     * <p>
     * {"empno":16,"ename":"蚩尤后裔","job":"clerk","mgr":7902,"hiredate":"1992-05-05 00:00:00","sal":8200,"comm":6200,"deptno":2}
     *
     * @param basPersonInfo 表单数据
     * @return
     */
    @PostMapping("/emp2/update")
    public ResultData<?> update(@RequestBody BasPersonInfo basPersonInfo) {
        personInfoService.update(basPersonInfo);
        return new ResultData<>();
    }

    /**
     * 根据id删除，在有逻辑删除字段的情况下，做UPDATE操作
     * http://localhost:8080/emp2/deleteById?id=10
     *
     * @param id 主键id
     * @return
     */
    @GetMapping("/emp2/deleteById")
    public ResultData<?> deleteById(@RequestParam Integer id) {
        personInfoService.deleteById(id);
        return new ResultData<>();
    }

}
